///////////////////////////////////////////////////////////
//  CCheckListView.cpp
//  Implementation of the Class CCheckListView
//  Created on:      21-Sep-2013 9:36:12 AM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "CCheckListView.h"
#include "ControlCommand.h"
#include "s3eEMail.h"
#include "stdafx.h"

using namespace UI;
using namespace UI::CMD;

CCheckListView::CCheckListView(IPages* checklistPages, IWindowDirector* director)
{
  this->m_type = IViewCtrl::CHECKLISTVIEW_T;
  this->m_checkListPages = checklistPages;
  this->m_parrentWnd = director;
  this->m_currentRow = -1;

  // call initialize for create all UI.
  Initialize();
}

CCheckListView::~CCheckListView()
{

}


UI::IViewCtrl::ViewType CCheckListView::GetType() const 
{
	return  this->m_type;
}


void CCheckListView::Initialize()
{
  this->SetAttribute("x1", 0);
  this->SetAttribute("y1", 0);
  this->SetAttribute("width", "100%");
  this->SetAttribute("height", "90%");
  this->SetAttribute("backgroundColour", CColour(COLOUR_PAGE));

  // Create navigationbar and set right button name

  this->m_navigationBar = CreateNavigationBar(CAttributes()
    .Set("name", "navBar")
    .Set("title", "Checklist")
    .Set("x1",      0)
    .Set("y1",      0)
    .Set("width",   "100%")
    .Set("height",  "10%"));

  ///////////////////////////////////////////////////////
  // Check operation system is support the email library
  // if it supports this library, then it will shown the send mail button.
  //////////////////////////////////////////////////////
  if(s3eEMailAvailable())
  {
    this->m_navigationBar->SetAttribute("rightButtonCaption", "Send mail");
  }

  // Setting event handler for handling navigation bar click
  this->m_navigationBar->SetEventHandler("clicknavigationbar", this, &CCheckListView::OnClickedNavigationBar);

  // Add navigation bar in view
  this->AddChild(this->m_navigationBar);

  CButtonPtr buttonItem = CreateButton(CAttributes()
    .Set("name", "buttonEdit")
    .Set("x1",    "0%")
    .Set("y1",    "11%")
    .Set("width", "33%")
    .Set("caption", "Edit"));

  this->AddChild(buttonItem);
  buttonItem->SetEventHandler("click", this, &CCheckListView::OnClickedEdit);

  buttonItem = CreateButton(CAttributes()
    .Set("name", "buttonDelete")
    .Set("x1",    "33%")
    .Set("y1",    "11%")
    .Set("width", "34%")
    .Set("caption", "Delete"));

  this->AddChild(buttonItem);
  buttonItem->SetEventHandler("click", this, &CCheckListView::OnClickedRemove);

  buttonItem = CreateButton(CAttributes()
    .Set("name", "buttonView")
    .Set("x1",    "67%")
    .Set("y1",    "11%")
    .Set("width", "33%")
    .Set("caption", "View"));

  this->AddChild(buttonItem);
  buttonItem->SetEventHandler("click", this, &CCheckListView::OnClickedView);

  // Create tool bar
  //this->m_toolBar = CreateToolBar(CAttributes()
  //  .Set("name",    "toolbar")
  //  .Set("x1",      "0%")
  //  .Set("y1",      "10%")
  //  .Set("width",   "100%")
  //  .Set("height",  "10%"));

  //// Create tool bar button for New, Edit, Remove and View
  //CToolBarButtonPtr toolButton = CreateToolBarButton(CAttributes()
  //  .Set("name",    "ToolBarEditItem")
  //  .Set("caption", "Edit"));

  //this->m_toolBar->AddChild(toolButton);

  //toolButton = CreateToolBarButton(CAttributes()
  //  .Set("name",    "ToolBarDeleteItem")
  //  .Set("caption", "Delete"));

  //this->m_toolBar->AddChild(toolButton);

  //toolButton = CreateToolBarButton(CAttributes()
  //  .Set("name",    "ToolBarViewItem")
  //  .Set("caption", "View"));

  //this->m_toolBar->AddChild(toolButton);

  //this->m_toolBar->SetEventHandler("clicktoolbar", this, &CCheckListView::OnClickedToolBar);

  //this->AddChild(this->m_toolBar);

  this->m_table = CreateTable(CAttributes()
    .Set("name",    "TablePages")
    .Set("x1",      0)
    .Set("y1",      "20%")
    .Set("width",   "100%")
    .Set("height",  "80%")
    .Set("alignH",  "top"));

  // Setting event handler for handling table selected row
  this->m_table->SetEventHandler("selectrow", this, &CCheckListView::OnSelectedTable);

  //if(this->m_templatePages)
  /*for(int rowIndex = 0; rowIndex < 10; rowIndex++)
  {
    CTableItemPtr tableItem = CreateTableItem(CAttributes()
      .Set("width",   "100%")
      .Set("height",  "50"));

    char tmp[0xff];
    snprintf(tmp, 0xff, "LabelTable%d", rowIndex);

    CLabelPtr label = CreateLabel(CAttributes()
      .Set("name",    CString(tmp))
      .Set("caption", CString(tmp))
      .Set("width", "100%")
      .Set("height", "100%")
      );

    tableItem->AddChild(label);

    this->m_table->AddChild(tableItem);

    this->m_tableRows.push_back(tableItem);
  }*/

  this->m_alretDlg = CreateAlertDialog(CAttributes()
    .Set("name",                    "AlertDialog")
    .Set("title",                   "Warning")
    .Set("message",                 "Would you need to delete this check list?")
    .Set("positiveButtonCaption",   "Yes")
    .Set("negativeButtonCaption",   "No"));


  this->m_alretDlg->SetEventHandler("dismissalertdialog", this, &CCheckListView::OnAlertButtonClick);


  Refresh();


  // Add table in view
  this->AddChild(this->m_table);

}


void CCheckListView::Refresh()
{
  //this->m_currentRow = -1;

  int iPages = this->m_checkListPages->CountPages();
  int iRows = (int)this->m_tableRows.size();
  int index = 0;
  // Remove all row in table
  for(index = 0; index < iRows; index++)
    this->m_table->RemoveChild(this->m_tableRows[index]);

  // **** Find the method to delete auto pointer ****
  this->m_tableRows.clear();

  for(index = 0; index < iPages; index++)
  {
    CTableItemPtr tableItem = CreateTableItem(CAttributes()
      .Set("width",   "100%")
      .Set("height",  "50")
      .Set("alignH",  "top"));

    char tmp[0xff];
    snprintf(tmp, 0xff, "LabelTable%d", index);

    CLabelPtr label = CreateLabel(CAttributes()
      .Set("name",    CString(tmp))
      .Set("caption", this->m_checkListPages->GetPage(index)->GetName().c_str())
      .Set("x1",      0)
      .Set("width", "100%")
      .Set("height", "100%")
      );

    tableItem->AddChild(label);

    ///*label = CreateLabel(CAttributes()
    //  .Set("name",    "abc")
    //  .Set("caption", std::string("testd" + this->m_checkListPages->GetPage(index)->GetName()).c_str())
    //  .Set("x1",      "60%")
    //  .Set("width", "30%")
    //  .Set("height", "100%")
    //  );

    //tableItem->AddChild(label);*/

    this->m_table->AddChild(tableItem);

    this->m_tableRows.push_back(tableItem);
  }

  /*if(iRows > 0)
  {
    this->m_table->SetAttribute("selected", 0);
    this->m_currentRow = 0;
  }*/
}


void CCheckListView::ShowView(bool show)
{
  this->SetAttribute("visible", show);
}


void CCheckListView::OnSelectedPage()
{

}


bool CCheckListView::OnClickedEdit(CButton* button)
{
  if(this->m_currentRow != -1)
  {
    ControlCommand::GetInstance()->OnEdit(ControlCommand::CHECKLIST_CTRL);
    this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATECHECKLISTVIEW_T);
  }
  return true;
}


void CCheckListView::OnClickedSendMail()
{

}


bool CCheckListView::OnClickedView(CButton* button)
{
  ControlCommand::GetInstance()->OnViewDetail(ControlCommand::CHECKLIST_CTRL); 
  this->m_parrentWnd->OnShowView(this, IViewCtrl::PREVIEWDETIALVIEW_T);
  return true;
}


bool CCheckListView::OnClickedRemove(CButton* button)
{
  if(this->m_currentRow != -1 && this->m_checkListPages->CountPages() > 0)
    this->m_alretDlg->Show();
  return true;
}


IPage* CCheckListView::GetCurSelected()
{
	return  this->m_checkListPages->GetPage(this->m_currentRow);
}

bool UI::CCheckListView::OnClickedNavigationBar( CNavigationBar *naviBar, int button )
{
  switch(button)
  {
  case S3E_NUI_NAVIGATION_BAR_BUTTON_LEFT:
    break;
  case S3E_NUI_NAVIGATION_BAR_BUTTON_RIGHT:
    {
      if(this->m_currentRow != -1)
        ControlCommand::GetInstance()->OnSendMail();
    }
    break;
  default: break;
  }
  return true;
}

bool UI::CCheckListView::OnClickedToolBar( CToolBar* toolBar, int button )
{
  switch(button)
  {
  case 0: // Edit button
    {
      if(this->m_currentRow != -1)
      {
        ControlCommand::GetInstance()->OnEdit(ControlCommand::CHECKLIST_CTRL);
        this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATECHECKLISTVIEW_T);
      }
    }
    break;
  case 1: // Remove button
    {
      //if(this->m_currentRow != -1 && this->m_templatePages->CountPages() > 0)
      {
        
        // Save file
        this->m_currentRow = -1;
        Refresh();
      }
    }
    break;
  case 2: // View button
    {
      ControlCommand::GetInstance()->OnViewDetail(ControlCommand::CHECKLIST_CTRL); 
      this->m_parrentWnd->OnShowView(this, IViewCtrl::PREVIEWDETIALVIEW_T);
    }
    break;
  default: break;
  }
  return true;
}

bool UI::CCheckListView::OnSelectedTable( CTable* table, int tab )
{
  this->m_currentRow = tab;
  return true;
}

void UI::CCheckListView::AppendPage( IPage* newPage )
{
  this->m_checkListPages->AppendPage(dynamic_cast<IPage*>(newPage->Clone()));
}

void UI::CCheckListView::EditPage( IPage* newPage )
{
  if(this->m_currentRow != -1)
  {
    IPage* currentPage = this->m_checkListPages->GetPage(this->m_currentRow);
    currentPage->ClearItems();
    currentPage->CopyPage(newPage);
  }
}

bool UI::CCheckListView::OnAlertButtonClick( CAlertDialog* dialog, EAlertDialogButton button )
{
  switch(button)
  {
  case IWNUI_ALERT_DIALOG_POSITIVE:
    {
      this->m_checkListPages->RemovePage(this->m_currentRow);
      // Save file
      this->m_table->RemoveChild(this->m_tableRows[this->m_currentRow]);
			this->m_tableRows.erase(this->m_tableRows.begin() + this->m_currentRow);
      this->m_currentRow = -1;
      //Refresh();
    }
    break;
  case IWNUI_ALERT_DIALOG_NEGATIVE:
    break;
  case IWNUI_ALERT_DIALOG_NEUTRAL:
    break;
  default: break;
  }
  return true;
}
